home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-06-23 | 5.2 KB | 175 lines |
- import java.awt.*;
- import java.util.Vector;
-
- class DefaultBorder extends BufferedDrawer
- {
- public DefaultBorder()
- {
- cornersImage = Util.loadImage("images/buttonCorners.gif", this);
-
- colors = new Vector();
- colors.addElement(Util.GSBColor); //Top and Left
- colors.addElement(Util.GSBColor); //Bottom and Right
- colors.addElement(Util.GS2Color); //Top and Left
- colors.addElement(Util.GS8Color); //Bottom and Right
- colors.addElement(Util.GS5Color); //Top and Left
- colors.addElement(Util.GS5Color); //Bottom and Right
-
- setLayout(null);
- }
-
- /**
- * Returns the preferred size of this component.
- * Overriden here to constrain height.
- * @see java.awt.Component#getMinimumSize
- * @see LayoutManager
- */
- public Dimension getPreferredSize()
- {
- Component components[] = getComponents();
- Component comp;
- Dimension s = null;
- for (int i = 0; i < components.length; ++i)
- {
- comp = components[i];
- if (comp instanceof java.awt.Button)
- {
- Dimension compSize = comp.getPreferredSize();
- s = new Dimension(compSize.width + defaultBorderInsets.left + defaultBorderInsets.right, compSize.height + defaultBorderInsets.top + defaultBorderInsets.bottom);
- return s;
- }
- }
-
- if (s == null)
- s = super.getPreferredSize();
-
- return s;
- }
-
- /**
- * Sets the layout manager for this container.
- * Overriden here to prevent setting the layout manager.
- * @param mgr the specified layout manager
- * @see #doLayout
- * @see #getLayout
- */
- public void setLayout(LayoutManager mgr)
- {
- super.setLayout(null);
- }
-
- /**
- * Does a layout on this Container. Most programs should not call
- * this directly, but should invoke validate instead.
- * @see #setLayout
- * @see #validate
- */
- public void doLayout()
- {
- Component components[] = getComponents();
- Component comp;
- for (int i = 0; i < components.length; ++i)
- {
- comp = components[i];
- if (comp instanceof java.awt.Button)
- {
- Dimension compSize = comp.getPreferredSize();
- comp.setBounds(defaultBorderInsets.top, defaultBorderInsets.left, compSize.width, compSize.height);
- return;
- }
- }
- }
-
- /**
- * Renders the progress bar in an offscreen buffer.
- * Renders the bar to indicate the current percent complete.
- * @see #paint
- * @see #setPercent
- * @see #getPercent
- */
- protected void draw()
- {
- //Make sure we give the super a chance to do what it needs to.
- super.draw();
-
- //Get the size of the ofscreen image.
- int imageWidth = workingImage.getWidth(this);
- int imageHeight = workingImage.getHeight(this);
-
- //Erase the contents.
- workingGraphics.setColor(getBackground());
- workingGraphics.fillRect(0, 0, imageWidth, imageHeight);
-
- if (cornersImage != null)
- paintButtonBorder(this, workingGraphics, cornersImage, colors, new Point(0, 0), getSize());
- }
-
- protected void paintButtonBorder(Component c, Graphics g, Image img, Vector colors, Point origin, Dimension size)
- {
- int halfWidth = img.getWidth(c) >> 1;
- int halfHeight = img.getHeight(c) >> 1;
- int w = size.width - 1;
- int h = size.height - 1;
- int x = origin.x;
- int y = origin.y;
-
- // Draw the corners
- paintButtonCorners(c, g, img, origin, size);
-
- Color oldColor = g.getColor();
-
- //Draw the lines
- for(int index = 0, count = 0; index < colors.size(); index += 2, ++count)
- {
- g.setColor((Color)colors.elementAt(index));
- g.drawLine(halfWidth + x, count + y, w - halfWidth + x, count + y); //Top
- g.drawLine(count + x, halfHeight + y, count + x, h - halfHeight + y); //Left
- try
- {
- g.setColor((Color)colors.elementAt(index + 1));
- }
- catch (ArrayIndexOutOfBoundsException exc) { }
- g.drawLine(halfWidth + x, h - count + y, w - halfWidth + x, h - count + y); //Bottom
- g.drawLine(w - count + x, halfHeight + y, w - count + x, h - halfHeight + y); //Right
- }
-
- g.setColor(oldColor);
- }
-
- protected void paintButtonCorners(Component c, Graphics g, Image img, Point origin, Dimension size)
- {
- int imgWidth = img.getWidth(c);
- int imgHeight = img.getHeight(c);
- int halfWidth = imgWidth >> 1;
- int halfHeight = imgHeight >> 1;
- int x = origin.x;
- int y = origin.y;
-
- //Draw the Top Left corner
- g.drawImage(img,
- x, y, halfWidth + x, halfHeight + y, /*destination*/
- 0, 0, halfWidth, halfHeight, /*source*/
- c);
- //Draw the Top Right corner
- g.drawImage(img,
- size.width - halfWidth + x, y, size.width + x, halfHeight + y, /*destination*/
- halfWidth, 0, imgWidth, halfHeight, /*source*/
- c);
- //Draw the Bottom Left corner
- g.drawImage(img,
- x, size.height - halfHeight + y, halfWidth + x, size.height + y, /*destination*/
- 0, halfHeight, halfWidth, imgHeight, /*source*/
- c);
- //Draw the Bottom Right corner
- g.drawImage(img,
- size.width - halfWidth + x, size.height - halfHeight + y, size.width + x, size.height + y, /*destination*/
- halfWidth, halfHeight, imgWidth, imgHeight, /*source*/
- c);
- }
-
- protected final static Insets defaultBorderInsets = new Insets(3, 3, 3, 3);
-
- protected Image cornersImage;
- protected Vector colors;
- }
-